/*
 * File     : ThunkFactory.java
 * Created  : 13 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.dni.callback;

import com.googlecode.dni.DirectNativeInterface;
import com.googlecode.dni.library.CallingConvention;
import com.googlecode.dni.type.Pointer;

/**
 * <p>
 *  Provides an abstract factory for the {@link FunctionPointer} type.
 * </p>
 *
 * @param <T>
 *            the delegate type; must be an interface annotated with
 *            {@link Callback}
 *
 * @see DirectNativeInterface#getFunctionPointerFactory(Class)
 *
 * @author Matthew Wilson
 */
public interface FunctionPointerFactory< T >
{

    /**
     * <p>
     *  Determines the calling convention of the returned function pointers.
     * </p>
     *
     * @return the calling convention
     */
    CallingConvention getCallingConvention();

    /**
     * <p>
     *  Creates a thunk for the given call-back.
     * </p>
     * <p>
     *  The thunk must be explicitly disposed when no longer required.  It is
     *  expected that using the same thunk multiple times will be orders of
     *  magnitude faster than using a vanilla call-back for each call (which
     *  requires the internals of DNI to create a thunk and destroy it for each
     *  and every call.)
     * </p>
     * <p>
     *  A thunk can be used concurrently from multiple threads, but it
     *  does not magically make the Java code it calls thread-safe.
     * </p>
     *
     * @param callback
     *            the Java call-back
     *
     * @return a thunk
     */
    FunctionPointer< T > create( T callback );

    /**
     * <p>
     *  Wraps a pointer, interpreting it as a function pointer to a call-back
     *  of this factory's type with this factory's calling convention.
     * </p>
     *
     * @param pointer
     *            the pointer
     *
     * @return a function pointer
     */
    FunctionPointer< T > wrap( Pointer pointer );

}
